package com.feign.header.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@Component
public class FeignHeaderRequestInterceptor implements RequestInterceptor {

    @Resource
    private HttpServletRequest request;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            Enumeration<String> headers = request.getHeaders(headerName);
            List<String> values = new ArrayList<>();
            while (headers.hasMoreElements()) {
                String headerValue = headers.nextElement();
                values.add(headerValue);
            }
            requestTemplate.header(headerName, values);
        }
    }
}
